﻿Public Class Passes
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'check authentication
        If AuthenticationValidator.CheckIfAuthorized(Me, "Passes - View") = False Then
            Me.Response.Redirect("AccessDeniedMsg.aspx")
        End If

        If Me.IsPostBack = False Then
            RefreshPasses()
            toggleButtonStates()
        End If

    End Sub

    Private Sub toggleButtonStates()

        Add_Button.Enabled = False
        Save_Button.Enabled = False
        Delete_Button.Enabled = False
        If Me.ViewState("SelectedPassId") = Guid.Empty Then
            If AuthenticationValidator.CheckIfAuthorized(Me, "Passes - Add") Then Add_Button.Enabled = True
        Else
            If AuthenticationValidator.CheckIfAuthorized(Me, "Passes - Add") Then Add_Button.Enabled = True
            If AuthenticationValidator.CheckIfAuthorized(Me, "Passes - Edit") Then Save_Button.Enabled = True
            If AuthenticationValidator.CheckIfAuthorized(Me, "Passes - Delete") Then Delete_Button.Enabled = True
        End If
        If (AuthenticationValidator.CheckIfAuthorized(Me, "Passes - Add") = False) _
            OrElse (AuthenticationValidator.CheckIfAuthorized(Me, "Passes - Edit") = False) _
            OrElse (AuthenticationValidator.CheckIfAuthorized(Me, "Passes - Delete") = False) Then
            info_Label.Text = "Some functionality of this screen, has been disabled, because you don't have sufficient rights. <br/>If you feel that this is an error, please contact the administrator of this system."
        Else
            If info_Label.Text = "Some functionality of this screen, has been disabled, because you don't have sufficient rights. <br/>If you feel that this is an error, please contact the administrator of this system." Then
                info_Label.Text = ""
            End If
        End If
    End Sub

    Private Sub RefreshPasses()

        Dim d As New DBHelper
        Passes_ListBox.DataSource = d.GetPassesList
        Passes_ListBox.DataTextField = "Text"
        Passes_ListBox.DataValueField = "PassId"
        Passes_ListBox.DataBind()

    End Sub

    Private Sub Passes_ListBox_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles Passes_ListBox.SelectedIndexChanged
        selectPass(Passes_ListBox.SelectedItem)
        toggleButtonStates()
        info_Label.Text = ""
    End Sub

    Private Sub selectPass(item As ListItem)

        Dim passId As Guid
        Dim pass As PassType = Nothing
        If item Is Nothing Then
            passId = Guid.Empty
        Else
            passId = New Guid(item.Value)
            Dim db As New DBHelper
            pass = db.GetPassById(passId)
        End If

        If passId = Guid.Empty Then
            Me.ViewState("SelectedPassId") = ""
            PassType_DropDownList.SelectedValue = "Permanent"
            PassNumber_TextBox.Text = ""
        Else
            Me.ViewState("SelectedPassId") = passId
            PassType_DropDownList.SelectedValue = pass.PassType
            PassNumber_TextBox.Text = pass.PassNumber
        End If

    End Sub

    Protected Sub Add_Button_Click(sender As Object, e As EventArgs) Handles Add_Button.Click

        Dim db As New DBHelper
        Dim pass As New PassType
        pass.PassId = Guid.NewGuid
        pass.PassType = PassType_DropDownList.SelectedValue
        pass.PassNumber = PassNumber_TextBox.Text

        Try
            db.AddPass(pass)
            RefreshPasses()
            info_Label.Text = ""
        Catch ex As SqlClient.SqlException
            If ex.Message.Contains("unique index") Then
                info_Label.Text = "Pass number already exists in the system, cannot insert duplicate pass numbers"
            Else
                info_Label.Text = ex.Message
            End If
        Catch ex As Exception
            info_Label.Text = ex.Message
        End Try

    End Sub

    Protected Sub Save_Button_Click(sender As Object, e As EventArgs) Handles Save_Button.Click

        Dim db As New DBHelper
        Dim pass As New PassType
        pass.PassId = Me.ViewState("SelectedPassId")
        pass.PassType = PassType_DropDownList.SelectedValue
        pass.PassNumber = PassNumber_TextBox.Text

        Try
            db.UpdatePass(pass)
            RefreshPasses()
        Catch ex As SqlClient.SqlException
            If ex.Message.Contains("unique index") Then
                info_Label.Text = "Pass number already exists in the system, cannot insert duplicate pass numbers"
            End If
        Catch ex As Exception
            info_Label.Text = ex.Message
        End Try

    End Sub

    Protected Sub Delete_Button_Click(sender As Object, e As EventArgs) Handles Delete_Button.Click

        Dim db As New DBHelper
        Dim pass As New PassType
        pass.PassId = Me.ViewState("SelectedPassId")

        Try
            db.Deletepass(pass)
            selectPass(Nothing)
            RefreshPasses()

        Catch ex As Exception
            info_Label.Text = ex.Message
        End Try

    End Sub

End Class